package com.sls.web.manager.game.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.web.manager.game.constants.ResultCode;
import com.sls.web.manager.game.pojo.ekhui.Deposit;
import com.sls.web.manager.game.pojo.ekhui.FundBalance;
import com.sls.web.manager.pojo.UserInfoVO;

public class FundBalanceClient {
	private static final Logger log = LoggerFactory.getLogger(FundBalanceClient.class) ;
	private static volatile FundBalanceClient instance = null ;
	private static Lock lock = new ReentrantLock() ;
	private FundBalanceClient(){
		
	}
	public static FundBalanceClient getInstance(){
		if(instance == null){
			try {
				lock.lock();
				if(instance == null){
					instance = new FundBalanceClient() ;
				}
			} finally{
				lock.unlock() ;
			}
		}
		return instance ;
	}
	public List<FundBalance> getFundBalanceList(FundBalance fundBalance , UserInfoVO userInfoVO ,String startTime,
			String endTime, int pageNo , int pageSize){
		List<FundBalance> list = null ;
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setServiceCode("300037");
		head.setUserId(userInfoVO.getUserId());
		head.setRequestId(UUID.randomUUID().toString());
		
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("fundBalance", fundBalance) ;
		paramMap.put("pageNo", pageNo) ;
		paramMap.put("pageSize", pageSize) ;
		paramMap.put("startTime", startTime) ;
		paramMap.put("endTime", endTime) ;
		SocketDTO socketDTO = SocketUtil.send(head, paramMap);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
	    	String body = socketDTO.getBody();
	    	if(StringUtils.isNotBlank(body)){
	    		Gson gson = new Gson();
	    		list = gson.fromJson(body , new TypeToken<List<FundBalance>>(){}.getType()); 
	    	}
	    }
		return list ;
	}
	public int getFundBalanceCount(FundBalance fundBalance , UserInfoVO userInfoVO ,String startTime,
			String endTime){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setServiceCode("300038");
		head.setUserId(userInfoVO.getUserId());
		head.setRequestId(UUID.randomUUID().toString());
		
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("fundBalance", fundBalance) ;
		paramMap.put("startTime", startTime) ;
		paramMap.put("endTime", endTime) ;
		SocketDTO socketDTO = SocketUtil.send(head, paramMap);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			if(StringUtils.isNotBlank(body)){
				Gson gson = new Gson() ;
				return gson.fromJson(body, new TypeToken<Integer>(){}.getType()) ;
			}
		}
		return 0 ;
	}
	public boolean saveFundBalance(FundBalance fundBalance , UserInfoVO userInfoVO){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setServiceCode("300015");
		head.setUserId(userInfoVO.getUserId());
		head.setRequestId(UUID.randomUUID().toString());
		SocketDTO socketDTO = SocketUtil.send(head, fundBalance);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			if(StringUtils.isNotBlank(body)){
				Gson gson = new Gson() ;
				return gson.fromJson(body, new TypeToken<Boolean>(){}.getType()) ;
			}
		}
		return false ;
	}
}
